// pages/my/index/index.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        userInfo: null,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        
        
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        let userInfo = wx.getStorageSync('userInfo');
        if(userInfo){
            this.getUserInfo();
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },
    //获取用户信息
    getUserInfo: function() {
        wx.$http({
            load: true,
            url: '/business/get-info',
            method: 'get',
            data: {}
        }).then((res) => {
            if(res.code == 1){
                let data = res.data;
                wx.setStorageSync('userInfo',data);
                this.setData({
                    userInfo: data
                })
            }
        })
    },
    //去门店设置
    goShop: function () {
        let {userInfo} = this.data;
        if (!userInfo || !userInfo.id){
            wx.navigateTo({
                url: '/pages/login/index',
            })
            return;
        }
        if (userInfo.shop_status == 2) {
            wx.navigateTo({
                url: '/pages/my/shop/index/index',
            })
        } else {
            wx.showModal({
                title: '提示',
                content: '您还没有完成入住，请先完成入驻',
                complete: (res) => {
                    if (res.confirm) {
                        wx.navigateTo({
                            url: '/pages/my/checkIn/index/index',
                        })
                    }
                }
            })
        }

    },
    //外卖管理
    goTakeOut: function(){
        let {userInfo} = this.data;
        if (!userInfo || !userInfo.id){
            wx.navigateTo({
                url: '/pages/login/index',
            })
            return;
        }
        if (userInfo.shop_status == 2) {
            wx.navigateTo({
                url: '/pages/takeOut/index/index',
            })
        } else{
            wx.showModal({
                title: '提示',
                content: '您还没有完成入住，请先完成入驻',
                complete: (res) => {
                    if (res.confirm) {
                        wx.navigateTo({
                            url: '/pages/my/checkIn/index/index',
                        })
                    }
                }
            })
        }
    },
    //团购管理
    goGroup: function() {
        let {userInfo} = this.data;
        if (!userInfo || !userInfo.id){
            wx.navigateTo({
                url: '/pages/login/index',
            })
            return;
        }
        if(userInfo.shop_status == 2) {
            wx.navigateTo({
                url: '/pages/groupBuy/index/index',
            })
        }else{
            wx.showModal({
                title: '提示',
                content: '请先完成入驻',
                complete: (res) => {
                    if (res.confirm) {
                        wx.navigateTo({
                            url: '/pages/my/checkIn/index/index',
                        })
                    }
                }
            })
        }
    },
    //核销订单
    goCheck: function() {
        let {userInfo} = this.data;
        if (!userInfo || !userInfo.id){
            wx.navigateTo({
                url: '/pages/login/index',
            })
            return;
        }
        wx.scanCode({
            success (res) {
                if(res.errMsg == 'scanCode:ok'){
                    let code = res.result;
                    wx.$http({
                        url: `/business/get-order-id/${code}`,
                        method: 'get',
                        data: {},
                    }).then((res) => {
                        let data = res.data;
                        wx.navigateTo({
                            url: `/pages/groupBuy/orderInfo/index?id=${data.id}&code=${code}`,
                        })
                    })
                }else{
                    wx.showToast({
                        title: '核销码错误',
                        icon: 'none'
                    })
                }
            }
        })
    },
    //去钱包页面
    goMoney: function() {
        let {userInfo} = this.data;
        if (!userInfo || !userInfo.id){
            wx.navigateTo({
                url: '/pages/login/index',
            })
            return;
        }
        wx.navigateTo({
            url: '/pages/my/money/total/index',
        })
        
    },
    //去用户端外卖店铺
    goUserTake: function() {
        let {userInfo} = this.data;
        if (!userInfo || !userInfo.id){
            wx.navigateTo({
                url: '/pages/login/index',
            })
            return;
        }
        wx.navigateToMiniProgram({
            appId: 'wxe5e03f583c6f321d',
            path: `/shop/pages/takeOut/shop/index?id=${userInfo.shop_id}`,
            envVersion: 'develop',
        })
    },
    //去用户端团购店铺
    goUserGroup: function() {
        let {userInfo} = this.data;
        if (!userInfo || !userInfo.id){
            wx.navigateTo({
                url: '/pages/login/index',
            })
            return;
        }
        wx.navigateToMiniProgram({
            appId: 'wxe5e03f583c6f321d',
            path: `/shop/pages/group/shop/index?id=${userInfo.shop_id}`,
            envVersion: 'develop',
        })
    },
    //去设置打印机
    goPrint: function() {
        let {userInfo} = this.data;
        if (!userInfo || !userInfo.id){
            wx.navigateTo({
                url: '/pages/login/index',
            })
            return;
        }
        wx.navigateTo({
            url: `/pages/my/print/index`,
        })
    }
})